summaryrefslogtreecommitdiff
path: root/src/pages/study/[slug].tsx
blob: a5af5231d573ddb8db7faabba390de4fb2369922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { getContextData } from "waku/middleware/context";
import { getState } from "@/lib/db";
import { startStudySession } from "@/actions/srs";
import StudySession from "@/components/Flashcard/StudySession";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import LessonSelector from "@/components/srs/LessonSelector";
import type { PageProps } from "waku/router";

// This is a server component that gets the initial data
export default async function StudyPage(props: PageProps<"/study/[slug]">) {
  const lessonId = props.slug;
  const ctx = getContextData() as any;
  const userId = ctx?.user?.id;
  // const state = getState(null);

  // If not logged in, show login required message
  if (!userId) {
    return (
      <div className="container mx-auto py-8">
        <Card className="p-6 text-center">
          <h1 className="text-2xl font-bold mb-4">Login Required</h1>
          <p className="mb-4">
            You need to be logged in to use the study session feature.
          </p>
          <Button asChild>
            <a href="/login">Login</a>
          </Button>
        </Card>
      </div>
    );
  }

  // If no lesson ID provided, show lesson selector

  // Get initial data for the study session

  return (
    <div className="container mx-auto py-8">
      <Inner userId={userId} lessonId={lessonId} />
    </div>
  );
}

async function Inner({
  userId,
  lessonId,
}: {
  userId: number;
  lessonId: string;
}) {
  return (
    <>
      {lessonId && Number(lessonId) ? (
        <StudySessionOuter userId={userId} lessonId={Number(lessonId)} />
      ) : (
        <LessonSelector userId={userId} />
      )}
    </>
  );
}
async function StudySessionOuter({
  userId,
  lessonId,
}: {
  userId: number;
  lessonId: number;
}) {
  const initialData = await startStudySession(userId, lessonId, true);
  console.log({ initialData });
  if ("ok" in initialData)
    return (
      <>
        <StudySession
          userId={userId}
          lessonId={lessonId}
          initialData={initialData.ok}
        />
      </>
    );
  else return <p>idk</p>;
}